/*
* MainThreadMessagePipe.cpp
*
*  Created on: 2013-3-3
*      Author: lmind
*/

#include "include.h"

/* Implementation file */
NS_IMPL_ISUPPORTS1(MainThreadMessagePipe, nsIMessagePipe);


MainThreadMessagePipe::MainThreadMessagePipe(nsIMessagePipe* wrapper)
{
	m_wrapper = wrapper;
}

MainThreadMessagePipe::~MainThreadMessagePipe()
{
	
}

/* readonly attribute wstring name; */
NS_IMETHODIMP MainThreadMessagePipe::GetName(PRUnichar * *aName)
{
	return m_wrapper->GetName(aName);
}

/* void addReceiver (in wstring title, in nsIMessageReceiver receiver); */
NS_IMETHODIMP MainThreadMessagePipe::AddReceiver(const PRUnichar * title, nsIMessageReceiver *receiver)
{
	return m_wrapper->AddReceiver(title, receiver);
}

/* void addGlobalReceiver (in nsIMessageReceiver receiver); */
NS_IMETHODIMP MainThreadMessagePipe::AddGlobalReceiver(nsIMessageReceiver *receiver)
{
	return m_wrapper->AddGlobalReceiver(receiver);
}

/* void removeReceiver (in wstring title, in nsIMessageReceiver receiver); */
NS_IMETHODIMP MainThreadMessagePipe::RemoveReceiver(const PRUnichar * title, nsIMessageReceiver *receiver)
{
	return m_wrapper->RemoveReceiver(title, receiver);
}

/* void removeGlobalReceiver (in wstring title, in nsIMessageReceiver receiver); */
NS_IMETHODIMP MainThreadMessagePipe::RemoveGlobalReceiver(const PRUnichar * title, nsIMessageReceiver *receiver)
{
	return m_wrapper->RemoveGlobalReceiver(title, receiver);
}

/* void send (in wstring title, in wstring data); */
NS_IMETHODIMP MainThreadMessagePipe::Send(const PRUnichar * title, const PRUnichar * data)
{
	nsCOMPtr<nsIRunnable> task = new MessageTask(title, data, m_wrapper);
	nsresult nr = NS_DispatchToMainThread(task);
	if (NS_FAILED(nr))
		return nr;
	
	return NS_OK;
}

void MainThreadMessagePipe::Wrap(nsIMessagePipe* pipe)
{
	m_wrapper = pipe;
}